package com.zhz.lib.project.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhz.lib.project.hibernate.Hotlink;
import com.zhz.lib.project.service.custom.CustomService;
import com.zhz.lib.project.service.hotlink.HotlinkService;
import com.zhz.lib.project.service.news.NewsService;
import com.zhz.lib.project.vo.custom.CustomVO;
import com.zhz.lib.project.vo.news.NewsVO;

@Controller
@RequestMapping("/index.htm")
public final class IndexController {
	private static Logger logger = Logger.getLogger(IndexController.class);
	private static int firstResult = 0;
	private static int maxResults = 8;
	@Autowired
	private NewsService newsService;
	@Autowired
	private HotlinkService hotlinkService;
	@Autowired
	private CustomService customService;

	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET)
	public String get(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		// 获取相关链接
		List<Hotlink> rlinkList = hotlinkService.getHotLink("rlink");
		model.put("rlinkList", rlinkList);

		// 获取常用链接
		List<Hotlink> hlinkList = hotlinkService.getHotLink("hlink");
		model.put("hlinkList", hlinkList);

		// 获取最新消息
		List<NewsVO> newsVOList = newsService.getLatestNews(firstResult,
				maxResults);
		model.put("newsVOList", newsVOList);

		// 获取顶级模块以及对应的子模块
		List<CustomVO> customVOList = customService.getTopCustom();
		if (customVOList != null && customVOList.size() > 0)
			for (CustomVO customVO : customVOList) {
				customVO.setSubCustomVOList(customService.getSubCustom(customVO
						.getCustomId()
						+ ""));
			}
		model.put("customVOList", customVOList);

		return "index";
	}

}
